package com.thlws.rocketmq;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class ProducerDemo {

	public static void main(String[] args) throws Exception {
       sendNormal();
    }
	
	public static void sendNormal() throws Exception {
        //Instantiate with a producer group name.
        DefaultMQProducer producer = new
            DefaultMQProducer("producer_orders_group");
        //Launch the instance.
//        producer.setVipChannelEnabled(false);//true的情况，默认会报错：connect to <172.16.1.1:10909> failed
        producer.setNamesrvAddr("47.100.122.135:9876");
        producer.setRetryTimesWhenSendFailed(10);//发送失败的重试行为
        producer.setVipChannelEnabled(false);
//        producer.setCreateTopicKey("AUTO_CREATE_TOPIC_KEY");
        producer.start();
        for (int i = 0; i < 2; i++) {
            //Create a message instance, specifying topic, tag and message body.
            Message msg = new Message("orders" /* Topic */,
                "tag1" /* Tag */,
                ("今天1117，我测试信息-> " +
                    i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
            );
            //Call send message to deliver message to one of brokers.
//            SendResult sendResult = producer.send(msg);
//            System.out.printf("%s%n", sendResult);
            
            producer.send(msg, new SendCallback() {
				public void onSuccess(SendResult sendResult) {
					System.out.println("send successful");
				}
				public void onException(Throwable e) {
					System.out.println("send failed:"+e.getMessage());
				}
            });
        }
        producer.shutdown();
    }
	
}
